﻿#include "evaluationreport.h"
#include "ui_evaluationreport.h"
#include <QPainter>
#include "currentuserdata.h"
#include "cdatabaseinterface.h"
#include <QDebug>
#include <QMessageBox>
evaluationReport::evaluationReport(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::evaluationReport),
    m_chart(NULL),
    m_chartview(NULL),
    m_axisX(NULL),
    m_axisY(NULL)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);      //设置无边框
    setAttribute(Qt::WA_TranslucentBackground,true);    //设置透明
    m_axisX=new QValueAxis;
    m_axisY=new QValueAxis;
    m_chart=new QChart;
    m_chartview=new QChartView(m_chart);

}

evaluationReport::~evaluationReport()
{

    if(m_chart)
        delete m_chart;
    if(m_chartview)
        delete m_chartview;
    if(m_axisX)
        delete m_axisX;
    if(m_axisY)
        delete m_axisY;
    delete ui;
}

void evaluationReport::on_noSave_pushButton_clicked()
{

    this->close();
}

void evaluationReport::on_save_pushButton_clicked()
{
    //保存图片
        QString fileName;
        fileName.append("./createFile/evaluateReport/report/");
        fileName.append(m_st_evaluatereport.reportDate.toString("yyyyMMddhhmmss")+".png");
        if(!ui->graphicsView_2->grab().save(fileName))
            qDebug()<<"报告图片保存失败";
        else {
            m_st_evaluatereport.backGroundPixmapPath = fileName;
        }
    //将数据存储到数据库中
    QString tableName("EvaluateReport");
    if(!CDatabaseInterface::getInstance()->insertRowTable(tableName,evaluateReportToVariantMap(m_st_evaluatereport)))
        qDebug()<<"训练报告存储失败"<<CDatabaseInterface::getInstance()->getLastError();
    else
    {
        QMessageBox::warning(NULL,tr("保存"),tr("报告保存成功"));

    }

    //将评估报告中的数据保存到评估记录中
    QString recordTable("EvaluateRecord");
    ST_EvaluateRecord st_evaluateRecord;
    st_evaluateRecord.ID = m_st_evaluatereport.ID;
    st_evaluateRecord.evaluateDate = m_st_evaluatereport.reportDate;
    st_evaluateRecord.evaluateDuration = m_st_evaluatereport.evaluateDuration;
    st_evaluateRecord.resistance = m_st_evaluatereport.resistance;

    st_evaluateRecord.maxAngle.append(QString::number(m_st_evaluatereport.frontMaxAngle) + "°;");
    st_evaluateRecord.maxAngle.append(QString::number(m_st_evaluatereport.backMaxAngle) + "°;");
    st_evaluateRecord.maxAngle.append(QString::number(m_st_evaluatereport.leftMaxAngle) + "°;");
    st_evaluateRecord.maxAngle.append(QString::number(m_st_evaluatereport.rightMaxAngle) + "°");

    if(!CDatabaseInterface::getInstance()->insertRowTable("EvaluateRecord",evaluateRecordToVariantMap(st_evaluateRecord)))
    {
        qDebug()<<"EvaluateRecord::on_createData_Btn_clicked"<<CDatabaseInterface::getInstance()->getLastError();
    }
    else
        qDebug()<<"评估记录保存成功";

    this->close();

}

void evaluationReport::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)
    QPainter painter(this);
    painter.fillRect(rect(),QColor(0,0,0,100));
}

void evaluationReport::evaluateAngleChart(const QList<QPointF> &pointList)
{

    m_axisX->setRange(-12, 12); //设置范围
    m_axisX->setLineVisible(true); //设置轴线和刻度线可见性
    m_axisX->setGridLineVisible(true); //设置网格线可见性

    m_axisY->setRange(-12, 12);       //设置范围
    m_axisY->setLineVisible(true); //设置轴线和刻度线可见性
    m_axisY->setGridLineVisible(true); //设置网格线可见性

    m_chart = ui->graphicsView_2->chart();
    m_chart->removeAllSeries();


    m_chart->addAxis(m_axisX, Qt::AlignBottom);  //将坐标轴加到chart上，居下
    m_chart->addAxis(m_axisY, Qt::AlignLeft);//居左
    QLineSeries *series = new QLineSeries;

    for(int i=0;i<pointList.size();i++)
    {
        QPointF point;
        point.setX(pointList.at(i).x());
        point.setY(pointList.at(i).y());
        series->append(point);

    }
    //将创建好的折线图series添加进chart中
    m_chart->addSeries(series);

}
void  evaluationReport::setReportData(const ST_EvaluateReport &st_evaluateReport,int type,QList<QPointF> &pointList)
{
    m_st_evaluatereport = st_evaluateReport;
    ST_PatientMsg st_patientMsg;
    st_patientMsg = CurrentUserData::getInstace()->getCurrentPatientMsg();
    ui->save_pushButton->setVisible(type);
    ui->noSave_pushButton->setVisible(type);
    //    m_st_evaluatereport=st_evaluatereport;

    ui->name_label->setText(st_patientMsg.userName);
    QString sex = st_patientMsg.sex == 1 ? tr("女") :tr("男");
    ui->sex_label->setText(sex);
    ui->height_label->setText(QString::number(st_patientMsg.height)+tr("cm"));
    ui->phone_label->setText(st_patientMsg.userPhone);
    ui->age_label->setText(QString::number(st_patientMsg.age) + tr("岁"));
    ui->remark_label->setText(st_patientMsg.remarkMessage);

    QDateTime m_dataTime;
    m_dataTime =QDateTime::currentDateTime();
    ui->reportDate_label->setText(m_dataTime.toString("yyyy-MM-dd"));
    int min = m_st_evaluatereport.evaluateDuration/60;
    int sec = m_st_evaluatereport.evaluateDuration%60;
    ui->assessTime_label->setText(QString("%1 min  %2 s").arg(min).arg(sec));
    ui->resistance_label->setText(QString::number(m_st_evaluatereport.resistance)+tr("档"));
    ui->frontAngle_label->setText(QString::number(m_st_evaluatereport.frontMaxAngle)+"°");
    ui->backAngle_label->setText(QString::number(m_st_evaluatereport.backMaxAngle)+"°");
    ui->leftAngle_label->setText(QString::number(m_st_evaluatereport.leftMaxAngle)+"°");
    ui->rightAngle_label->setText(QString::number(m_st_evaluatereport.rightMaxAngle)+"°");


    if(1 == type)//生成
    {
        ui->stackedWidget->setCurrentIndex(0);
    evaluateAngleChart(pointList);
     ui->save_pushButton->setVisible(true);
        ui->noSave_pushButton->setVisible(true);
        ui->confirm_pushButton->setVisible(false);
    }
    else if(2 == type)//加载图片
    {
        ui->save_pushButton->setVisible(false);
        ui->noSave_pushButton->setVisible(false);
        ui->confirm_pushButton->setVisible(true);

        QPixmap pix;
        pix.load(m_st_evaluatereport.backGroundPixmapPath);

        ui->report_Label_2->setPixmap(pix);
        ui->stackedWidget->setCurrentIndex(1);
    }
    this->show();
}

ST_EvaluateReport evaluationReport::getReportDataByStartTime(QString reportDateStr)
{
    ST_EvaluateReport st_evaluateReport;
    QString queryStr(QString("select * from EvaluateReport where reportDateStr = '%1'").arg(reportDateStr));
    if(CDatabaseInterface::getInstance()->exec(queryStr))
    {
        if(CDatabaseInterface::getInstance()->getValuesSize() > 0)
        {
            st_evaluateReport = variantMapToEvaluateReport(CDatabaseInterface::getInstance()->getValues(0,1).at(0));
        }
        else
            qDebug()<<"EvaluateReport::getReportDataByStartTime:未查到符合要求数据";
    }
    else
        qDebug()<<"EvaluateReport::getReportDataByStartTime"<<CDatabaseInterface::getInstance()->getLastError();

    return st_evaluateReport;
}

void evaluationReport::on_confirm_pushButton_clicked()
{
    this->close();
}

void evaluationReport::changeEvent(QEvent* event)
{
    switch (event->type())
    {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        QWidget::changeEvent(event);
        break;
    }
}
